<?php
/**
 * Created by PhpStorm.
 * User: yellow
 * Date: 2019/6/25
 * Time: 14:08
 */

namespace app\admin\logic;
use app\admin\logic\code\ErrorCode;
use app\common\model\Site;
use app\common\model\User;
use app\common\model\Admin;
use app\vehicle\model\Practice;
use app\vehicle\model\Volunteer;
class SiteLogic extends BaseLogic
{

    private $SiteModel;

    public function __construct()
    {
        parent::__construct();
        $this->SiteModel = new Site();
    }

    public function cateList($tempWhere, $nowPage){
        if ($tempWhere['name']){
            $where[] = ['name', 'like', "%".$tempWhere['name']."%"];
        }
        if (is_numeric($tempWhere['status']) === true){
            $where[] = ['status', '=', $tempWhere['status']];
        }
        $count = Site::where($where)->count();
        if ($count <= 0){
            return ajaxArr(ErrorCode::$LISTNULL, '列表为空');
        } else{
            $cateList = Site::where($where)->page($nowPage, $tempWhere['limit'])->order('id desc')->select();
            foreach($cateList as $k=>$v){
                $v['numbers'] = Volunteer::where('oid','eq',$v['id'])->count();
                $cateList[$k]['a_num'] = Practice::where('site_id','eq',$v['id'])->count();
            }
            return ajaxArr(ErrorCode::$SUCCESS, 'success', $cateList, $count);
        }
    }

    /**
     * 分类信息
     */
    public function cateInfo($id){
        $cateInfo = $this->SiteModel->get($id);
        if ($cateInfo){
            return ajaxArr(ErrorCode::$SUCCESS, 'success',$cateInfo);
        } else{
            return ajaxArr(ErrorCode::$INFONULL, '分类信息为空');
        }
    }

    public function handle($data){
        $act = $data['act'];
        unset($data['act']);
        $res = false;
        $info = Site::get($data['id']);
        switch ($act){
            case 'add':
                $res = $this->SiteModel->allowField(true)->save($data);

                $insert['name'] = $data['name'];
                $insert['nick_name'] = $data['name'];
                $insert['salt'] = rand(1000, 9999);
                $insert['password'] = pwdMd5('123456', $insert['salt']);
                $insert['create_time'] = time();
                $insert['status'] = 1;
                $insert['role_id'] = 5;
                $insert['site_id'] = $this->SiteModel->id;
                Admin::insert($insert);

                break;
            case 'edit':
                if($data['user_id'] && $data['status']==1 ){
                    User::where('team_admin','eq',$data['id'])->update(['team_admin'=>0]);
                    User::where('id','eq',$data['user_id'])->update(['team_admin'=>$data['id']]);

                    Volunteer::where('user_id','eq',$data['user_id'])->update(['site_id'=>$data['id']]);
                }else{
                    User::where('team_admin','eq',$data['id'])->update(['team_admin'=>0]);
                }
                $check = Admin::where('site_id','eq',$data['id'])->count();
                if($check){
                    Admin::where('site_id','eq',$data['id'])->update(['name'=>$data['name']]);
                }else{
                    $insert['name'] = $data['name'];
                    $insert['nick_name'] = $data['name'];
                    $insert['salt'] = rand(1000, 9999);
                    $insert['password'] = pwdMd5('123456', $insert['salt']);
                    $insert['create_time'] = time();
                    $insert['status'] = 1;
                    $insert['role_id'] = 5;
                    $insert['site_id'] = $data['id'];
                    Admin::insert($insert);
                }
                $res = $this->SiteModel->allowField(true)->save($data,['id' => $data['id']]);
                break;
            case 'del':
                $cateInfo = Site::get($data['id']);
                $res = $cateInfo->delete();
                break;
            default:
                break;
        }
        if ($res){
            return ajaxArr(ErrorCode::$SUCCESS, '操作成功');
        } else{
            return ajaxArr(ErrorCode::$OPERATIONERROR, '操作失败');
        }
    }

    public function getAllCate(){
        $where[] = ['status', '=', 1];
        $cateList = $this->SiteModel->where($where)->select();
        if ($cateList){
            return ajaxArr(ErrorCode::$SUCCESS, 'success', $cateList);
        } else{
            return ajaxArr(ErrorCode::$LISTNULL, '列表为空');
        }
    }
}